/**
 * Company:wz
 * Title:VerifyDealActivity.java
 * Description:
 * Date:2017年2月13日上午10:35:07
 * author:wei 
 * Version:V1.0
 */

package com.wz.datachannel.view;

import java.util.Timer;
import java.util.TimerTask;

import com.wz.datachannel.R;
import com.wz.datachannel.model.VerifyBean;
import com.wz.datachannel.service.MainService;
import com.wz.datachannel.util.FastJsonUtil;
import com.wz.datachannel.util.ViewUtil;
import com.wz.datachannel.util.WzTimer.TimeCall;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/** 
 * 验证登录申请处理Activity
 * @Description
 * @author wei 
 * @Date 2017年2月13日上午10:35:07
 */
public class VerifyDealActivity extends BaseActivity implements OnClickListener {

	public static final String INTENT_VERIFY_DATA = "verifyData";

	private TextView mDeviceName_tv;
	private TextView mClientName_tv;
	private TextView mFromAddr_tv;
	private View mYes_btn;
	private Button mNo_btn;

	private Intent mIntent;
	private VerifyBean mVerifyBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setAllowWindowMove(true, false);
		setContentView(R.layout.activity_verify_deal);

		mIntent = getIntent();
		findViewById();
		initListener();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		mIntent = intent;
	}

	@Override
	protected void onResume() {
		super.onResume();
		getIntentData();
		initView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		setTimerTaskStop();
	}

	private void getIntentData() {
		try {
			String data = mIntent.getStringExtra(INTENT_VERIFY_DATA);
			mVerifyBean = FastJsonUtil.parseJsonToBean(data, VerifyBean.class);
		} catch (Exception e) {
		}
		if (null == mVerifyBean) {
			finish();
		}
	}

	private void findViewById() {
		mDeviceName_tv = (TextView) findViewById(R.id.verify_device_tv);
		mClientName_tv = (TextView) findViewById(R.id.verify_clientName_tv);
		mFromAddr_tv = (TextView) findViewById(R.id.verify_fromAddr_tv);
		mYes_btn = findViewById(R.id.verify_yes_btn);
		mNo_btn = (Button) findViewById(R.id.verify_no_btn);
	}

	private void initListener() {
		mYes_btn.setOnClickListener(this);
		mNo_btn.setOnClickListener(this);
	}

	private void initView() {
		ViewUtil.showTextNormal(mDeviceName_tv, mVerifyBean.getDevice());
		ViewUtil.showTextNormal(mClientName_tv, mVerifyBean.getClientname());
		ViewUtil.showTextNormal(mFromAddr_tv, mVerifyBean.getFromaddr());

		setTimerTaskStop();
		setTimerTaskStart();
	}

	private Timer mTimer = null;
	private long mWaitTime = 0L;

	private void setTimerTaskStart() {
		if (mVerifyBean.getWaittime() == null || mVerifyBean.getWaittime() < 0) {
			mWaitTime = 0L;
		} else {
			mWaitTime = mVerifyBean.getWaittime();
		}
		mTimer = new Timer();
		mTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				mWaitTime -= 1000;
				mHandler.sendEmptyMessage(HANDLER_WHAT_UPDATE_TIME);
			}
		}, 0, 1000);
	}

	private void setTimerTaskStop() {
		if (null != mTimer) {
			mTimer.cancel();
			mTimer = null;
		}
	}

	private static final int HANDLER_WHAT_UPDATE_TIME = 1;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HANDLER_WHAT_UPDATE_TIME:
				long tt = mWaitTime / 1000;
				if (tt <= 0) {
					finish();
				} else {
					StringBuilder sb = new StringBuilder();
					sb.append("拒绝(").append(tt).append(")");
					ViewUtil.showTextNormal(mNo_btn, sb.toString());
				}
				break;

			default:
				break;
			}
		}

	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.verify_yes_btn:
			try {
				MainService.getDataChannel().sendConfirmVerifyResponse(mVerifyBean.getTaskid(), true, "同意");
			} catch (Exception e) {
				e.printStackTrace();
			}
			finish();
			break;
		case R.id.verify_no_btn:
			try {
				MainService.getDataChannel().sendConfirmVerifyResponse(mVerifyBean.getTaskid(), false, "拒绝登录");
			} catch (Exception e) {
				e.printStackTrace();
			}
			finish();
			break;
		default:
			break;
		}
	}

}
